# application/services/detection/product_model.py
from application.mapper.detection.product_model import ProductModelMapper
from typing import Dict, Any, List, Tuple
from application.models.detection.product_model import ProductModel
from application.utils.res import models_to_dicts_camel, to_snake_case


class ProductModelService:
    """产品型号服务层"""

    @staticmethod
    def get_product_model_list(
        page_num: int, page_size: int, conditions: Dict[str, Any] = None
    ) -> Tuple[List[Dict[str, Any]], int]:
        """
        分页获取产品型号数据列表
        :param page_num: 页码
        :param page_size: 每页数量
        :param conditions: 查询条件
        :return: (产品型号数据列表, 总数)
        """
        product_model_list, total = ProductModelMapper.get_product_model_list(
            page_num, page_size, conditions
        )

        result_list = models_to_dicts_camel(product_model_list)

        return result_list, total

    @staticmethod
    def get_product_model_optionselect() -> List[Dict[str, Any]]:
        """
        获取产品型号下拉列表
        :return: 产品型号列表
        """
        dict_types = ProductModelMapper.get_all_product_models()

        result_list = models_to_dicts_camel(dict_types)

        return result_list

    @staticmethod
    def create_product_model(product_model_data: Dict[str, Any]) -> Dict[str, Any]:
        """
        创建产品型号
        :param product_model_data: 产品型号数据
        :return: 创建后的产品型号数据
        """
        # 创建产品型号对象
        product_model = ProductModel()

        for key, value in product_model_data.items():

            snake_key = to_snake_case(key)
            if hasattr(product_model, snake_key):
                setattr(product_model, snake_key, value)
        
        created_model = ProductModelMapper.create_product_model(product_model)
        return models_to_dicts_camel([created_model])[0]

    @staticmethod
    def update_product_model(product_model_id: int, product_model_data: Dict[str, Any]) -> Dict[str, Any]:
        """
        更新产品型号
        :param product_model_id: 产品型号ID
        :param product_model_data: 产品型号数据
        :return: 更新后的产品型号数据
        """
        product_model = ProductModelMapper.get_product_model_by_id(product_model_id)
        
        if not product_model:
            return None
            
        # 更新产品型号对象
        for key, value in product_model_data.items():
            snake_key = to_snake_case(key)
            if hasattr(product_model, snake_key):
                setattr(product_model, snake_key, value)
        
        updated_model = ProductModelMapper.update_product_model(product_model)
        return models_to_dicts_camel([updated_model])[0]


    @staticmethod
    def delete_product_model(product_model_id: int) -> bool:
        """
        删除产品型号
        :param product_model_id: 产品型号ID
        :return: 删除结果 True/False
        """
        product_model = ProductModelMapper.get_product_model_by_id(product_model_id)
        
        if not product_model:
            return False
            
        try:
            ProductModelMapper.delete_product_model(product_model)
            return True
        except Exception:
            return False

    